<?php
namespace Common\Model;
use Think\Model\RelationModel;
class UsersModel extends RelationModel{
	
    /**
    * 通过邮箱来注册
    */
    public function regByEmail($email,$password,$status){
        //检测邮箱
        $pattern="/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
        if(!preg_match($pattern,$email)){
            $result = array(
                'code' => -3,
                'msg-zh-cn' => '邮箱格式不正确',
                'msg-en-us' => 'The email format is incorrect!'
            );
            return $result;
        } 
        //检测邮箱是否已经存在
        $map = array();
        $map['email'] = $email;
        
        $user = $this->where($map)->find();
        //status = 0 表示没有激活
        if($user && $user['status'] == 0){
            $result = array(
                'code' => -1,
                'msg-zh-cn' => '邮箱已经注册，但是尚未激活',
                'msg-en-us' => 'Email has been registered , but has not activated , you can try to activate this email',
                'user' => $user
            );
            return $result;
        }

        if($user && $user['status'] > 0){
            $result = array(
                'code' => -2,
                'msg-zh-cn' => '邮箱已经被注册，请更换邮箱或直接登录',
                'msg-en-us' => 'Email has bean registered , please change another email or login directly by this email'
                
            );
            return $result;
        }

        $data = array(
            'email' => $email,
            'status' => $status,
            'create_time' => Date('Y-m-d H:i:s'),
            'update_time' => Date('Y-m-d H:i:s')
        );
        //自动生成一个账户
        $username = explode("@",$email);
        $username = $username[0];
        $data['username'] = $this->createUsername($username,get_rand_num(3)); //随机生成一个账户名
        $salt = substr(uniqid(rand()), -6);//仿Ucenter加密方式，生成一个随机数字
        $data['salt'] = $salt;
        $data['password'] = md5(md5($password).$salt);
        $uid = $this->add($data);
        if($status == 0){
            $result = array(
                'code' => 1,
                'msg-zh-cn' => '基础信息注册成功，需要验证邮箱',
                'msg-en-us' => 'Base info register success , please activate your email',
                'user' => $this->find($uid)
            );
        }else{
            $result = array(
                'code' => 2,
                'msg-zh-cn' => '注册成功',
                'msg-en-us' => 'Register success',
                'user' => $this->find($uid)
            );
        }
        return $result;
    }



    /**
    * 创建一个可以用的用户名
    */
    public function createUsername($baseUsername,$num){
        
        if(!$this->checkUsernameIsExist($baseUsername)){
            return $baseUsername;
        }else{
            $username = $baseUsername.'_'.$num;//加随机数
            if(!$this->checkUsernameIsExist($username)){
                return $username;
            }else{
                return $this->createUsername($baseUsername,get_rand_num(3)); //递归出来
            }
        }
    }

    /**
    * 检测用户名是否已经存在
    */
    public function checkUsernameIsExist($username){
        $map['username'] = $username;
        if($this->where($map)->find()){
            return true;
        }else{
            return false;
        }
    }

}